library(estimatr)
library(ggplot2)
library(plyr)
library(dplyr)
library(dotwhisker)
library(gridExtra)
library(tidyverse)

# Figure 3: Cross-National Retaliation Preferences


setwd("/Users/dmin/Dropbox/Replication_Data/Paper_1/")

COMdat <- read.csv("Paper_1_Data_Clean_tidy.csv", stringsAsFactors = FALSE)

# Summarizing the data
COMdat11_alt <- 
  COMdat %>%
  group_by(Nationality, Japan, Nuclear) %>%
  summarize(tidy(lm_robust(cbind(NuclearMissile, Conventional, Nonmilitary) ~ 1, data = cur_data())))

# Labeling the Nationalities
NationalityLabeller <- as_labeller(c(
  '1' = "USA",            
  '2' = "Japan",         
  '3' = "S. Korea"
))

# Generating the figures

plot1 <- # Conventional attack on Busan, South Korea
  ggplot(subset(COMdat11_alt, Nuclear==0 & Japan==0), aes(x=outcome,y=estimate, fill = factor(Nationality))) +
  coord_cartesian(ylim = c(0, .6)) +
  ggtitle("Preferred response to Conventional Attack on Busan, South Korea") + ylab("Proportion of Respondents") +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
                width = .2,
                position = position_dodge(.9)) +
  geom_text(aes(label = round(estimate, 2)), y = 0.05) +
  scale_fill_manual(guide = "none",
                    values = c("forestgreen", "indianred2", "steelblue2")) +
  scale_x_discrete(name = NULL) +
  facet_wrap( ~ Nationality, labeller = NationalityLabeller) +
  theme_bw() +
  theme(axis.text.x = element_text(angle=90),
        legend.position = "none",
        plot.title = element_text(hjust = 0.5, size = 9))


plot2 <- # Nuclear attack on Busan, South Korea
  ggplot(subset(COMdat11_alt, Nuclear==1 & Japan==0), aes(x=outcome,y=estimate, fill = factor(Nationality))) +
  coord_cartesian(ylim = c(0, .6)) +
  ggtitle("Preferred response to Nuclear Attack on Busan, South Korea") + ylab("") +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
                width = .2,
                position = position_dodge(.9)) +
  geom_text(aes(label = round(estimate, 2)), y = 0.05) +
  scale_fill_manual(guide = "none",
                    values = c("forestgreen", "indianred2", "steelblue2")) +
  scale_x_discrete(name = NULL) +
  facet_wrap( ~ Nationality, labeller = NationalityLabeller) +
  theme_bw() +
  theme(axis.text.x = element_text(angle=90),
        legend.position = "none",
        plot.title = element_text(hjust = 0.5, size = 9))


plot3 <- # Conventional attack on Nagoya, Japan
  ggplot(subset(COMdat11_alt, Nuclear==0 & Japan==1), aes(x=outcome,y=estimate, fill = factor(Nationality))) +
  coord_cartesian(ylim = c(0, .6)) +
  ggtitle("Preferred response to Conventional Attack on Nagoya, Japan") + ylab("Proportion of Respondents") +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
                width = .2,
                position = position_dodge(.9)) +
  geom_text(aes(label = round(estimate, 2)), y = 0.05) +
  scale_fill_manual(guide = "none",
                    values = c("forestgreen", "indianred2", "steelblue2")) +
  scale_x_discrete(name = NULL) +
  facet_wrap( ~ Nationality, labeller = NationalityLabeller) +
  theme_bw() +
  theme(axis.text.x = element_text(angle=90),
        legend.position = "none",
        plot.title = element_text(hjust = 0.5, size = 9))


plot4 <- # Nuclear attack on Nagoya, Japan
  ggplot(subset(COMdat11_alt, Nuclear==1 & Japan==1), aes(x=outcome,y=estimate, fill = factor(Nationality))) +
  coord_cartesian(ylim = c(0, .6)) +
  ggtitle("Preferred response to Nuclear Attack on Nagoya, Japan") + 
  ylab("") +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
                width = .2,
                position = position_dodge(.9)) +
  geom_text(aes(label = round(estimate, 2)), y = 0.05) +
  scale_fill_manual(guide = "none",
                    values = c("forestgreen", "indianred2", "steelblue2")) +
  scale_x_discrete(name = NULL) +
  facet_wrap( ~ Nationality, labeller = NationalityLabeller) +
  theme_bw() +
  theme(axis.text.x = element_text(angle=90),
        legend.position = "none",
        plot.title = element_text(hjust = 0.5, size = 9))

grid.arrange(plot1, plot2, plot3, plot4, ncol=2)

g <- grid.arrange(plot1, plot2, plot3, plot4, ncol=2)

ggsave("Image_3.png", plot=g, width=2500, height=2500, units=c("px"))

